package tau.facebook;

import java.util.Map;

import javax.swing.event.EventListenerList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import tau.pimpmyfreedb.common.Network;

public class FacebookBrowser extends Composite {
    private final Browser browser;
    private final FacebookAPI fb;
    private EventListenerList listenersList;

    // constructor

    public FacebookBrowser(Shell shell, Composite parent, Object layoutData) {
	// instantiate
	super(parent, SWT.NONE);
	this.browser = new Browser(this, SWT.NONE);
	this.fb = new FacebookAPI();
	this.listenersList = new EventListenerList();

	// set layout
	this.setLayout(new GridLayout(1, true));
	this.setLayoutData(layoutData);
	this.browser.setLayoutData(layoutData);

	// set browser location listener
	this.browser.addLocationListener(new LocationListener() {

	    @Override
	    public void changing(LocationEvent arg0) {
	    }

	    @Override
	    public void changed(LocationEvent arg0) {
		BrowserLocationChanged(arg0);
	    }
	});

	// add standard widget listeners
	this.addDisposeListener(new DisposeListener() {
	    @Override
	    public void widgetDisposed(DisposeEvent arg0) {
		browser.dispose();
	    }
	});
    }

    // misc

    public void gotoLoginPage() {
	browser.setUrl(FacebookAPI.getLoginURL());
    }

    private void BrowserLocationChanged(LocationEvent arg0) {

	// get the current query string
	Map<String, String> query = Network.GetQueryString(arg0.location);

	// get access token
	if (query.containsKey("access_token")) {
	    // save access token in the FacebookAPI object
	    String token = query.get("access_token");
	    // long expirationTime = Long.parseLong(query.get("expires_in"));
	    fb.setToken(token);
	    // go back to the login screen
	    Browser.clearSessions();
	    browser.setUrl(FacebookAPI.getLoginURL());
	    // notify complete
	    NotifyLoginComplete();
	}
    }

    // getters

    public Browser GetBrowser() {
	return this.browser;
    }

    public FacebookAPI GetFacebookAPIObject() {
	return fb;
    }

    // LoginCompleteEvent

    public void addLoginListener(LoginCompleteListener listener) {
	listenersList.add(LoginCompleteListener.class, listener);
    }

    public void removeLoginListener(LoginCompleteListener listener) {
	listenersList.remove(LoginCompleteListener.class, listener);
    }

    private void NotifyLoginComplete() {
	LoginCompleteListener[] listeners = listenersList
		.getListeners(LoginCompleteListener.class);
	for (LoginCompleteListener lis : listeners) {
	    lis.onLoginComplete(this);
	}
    }
}
